/**
 * 
 */
package org.swing.utility.common.codec;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

/**
 * @author lqnhu
 *
 */
public class URLEncoderOutputStream extends OutputStream {
	protected final Appendable out;
	protected final ByteBuffer singleByteBuffer = ByteBuffer.allocate(1);

	/**
	 * Encodes to the specified {@link Appendable}.
	 */
	public URLEncoderOutputStream(Appendable out) {
		this.out = out;
	}

	@Override
	public void write(int b) throws IOException {
		singleByteBuffer.put(0, (byte) b);
		URLCodec.encode(singleByteBuffer, out);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		URLCodec.encode(ByteBuffer.wrap(b, off, len), out);
	}
}
